#include "../../../common/icsearch.hpp"
#include <gflags/gflags.h>

DEFINE_int32(run_mode, 0, "程序的运行模式,0-调试,1-发布");
DEFINE_string(log_file, "", "发布模式下,用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下,用于指定日志的输出等级");

int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    hdp::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    std::vector<std::string> host_list = {"http://127.0.0.1:9200/"};
    std::shared_ptr<elasticlient::Client> client = std::make_shared<elasticlient::Client>(host_list);

    // 创建索引
    bool ret = hdp::ESIndex(client, "test_user").append("nickname").append("phone", "keyword", "standard", true).create();
    if (ret == false)
    {
        LOG_ERROR("创建索引失败");
        return -1;
    }
    else
    {
        LOG_DEBUG("创建索引成功");
    }

    // 新增数据
    ret = hdp::ESInsert(client, "test_user").append("nickname", "张三").append("phone", "123456").insert("0001");
    if (ret == false)
    {
        LOG_ERROR("新增数据失败");
        return -1;
    }
    else
    {
        LOG_DEBUG("新增数据成功");
    }

    ret = hdp::ESInsert(client, "test_user").append("nickname", "李四").append("phone", "112233").insert("0002");
    if (ret == false)
    {
        LOG_ERROR("新增数据失败");
        return -1;
    }
    else
    {
        LOG_DEBUG("新增数据成功");
    }

    std::this_thread::sleep_for(std::chrono::seconds(1));
    // 检索数据
    Json::Value user = hdp::ESSearch(client, "test_user").append_should_match("nickname", "李四").search();
    if (user.empty() || user.isArray() == false)
    {
        LOG_ERROR("检索结果为空,或者结果不是数组类型");
        return -1;
    }
    else
    {
        LOG_DEBUG("数据检索成功");
    }
    int size = user.size();
    for (int i = 0; i < size; ++i)
    {
        LOG_INFO("nickname: {}", user[i]["_source"]["nickname"].asString());
        LOG_INFO("phone: {}", user[i]["_source"]["phone"].asString());
    }

    // 更新数据
    ret = hdp::ESInsert(client, "test_user").append("nickname", "李四").append("phone", "123456789").insert("0002");
    if (ret == false)
    {
        LOG_ERROR("更新数据失败");
        return -1;
    }
    else
    {
        LOG_DEBUG("更新数据成功");
    }

    std::this_thread::sleep_for(std::chrono::seconds(1));
    user = hdp::ESSearch(client, "test_user").append_should_match("phone.keyword", "123456789").search();
    if (user.empty() || user.isArray() == false)
    {
        LOG_ERROR("检索结果为空,或者结果不是数组类型");
        return -1;
    }
    else
    {
        LOG_DEBUG("数据检索成功");
    }
    size = user.size();
    for (int i = 0; i < size; ++i)
    {
        LOG_INFO("nickname: {}", user[i]["_source"]["nickname"].asString());
        LOG_INFO("phone: {}", user[i]["_source"]["phone"].asString());
    }

    // 删除数据
    ret = hdp::ESRemove(client, "test_user").remove("0002");
    if (ret == false)
    {
        LOG_ERROR("删除数据失败");
        return -1;
    }
    else
    {
        LOG_DEBUG("删除数据成功");
    }

    std::this_thread::sleep_for(std::chrono::seconds(1));
    user = hdp::ESSearch(client, "test_user").append_should_match("phone.keyword", "123456789").search();
    if (user.empty() || user.isArray() == false)
    {
        LOG_ERROR("检索结果为空,或者结果不是数组类型");
        return -1;
    }
    else
    {
        LOG_DEBUG("数据检索成功");
    }
    size = user.size();
    for (int i = 0; i < size; ++i)
    {
        LOG_INFO("nickname: {}", user[i]["_source"]["nickname"].asString());
        LOG_INFO("phone: {}", user[i]["_source"]["phone"].asString());
    }
    return 0;
}